#include "public.h"
#include "product_main.h"
#include "user_main.h"
#include "user_inf_aduf.h"
#include "matrl.h"
elem_type *log_user_check(vector v);
int startup_self_test(void);
void one_start_up();

// void user_inf_upd(union elem_vector *user_inf);

// 两份临时测试代码
int ceshi()
{
    FILE *fp = fopen(PRODUCT_DATA_FILE, "w");
    fclose(fp);
    fp = fopen(USER_DATA_FILE, "w");
    fclose(fp);

    // char upd[3] = "aa";
    // Upd_file_all(upd, "ceshi_read.txt", 2, 2);
    // FILE *fp = fopen("ceshi_read.txt", "a");

    // char *siz = (char *)malloc(21);
    // strcpy(siz, "abcdefaccdcdacac");
    // char upd[3] = "aa";
    // // fwrite(siz, 2, 1, fp);
    // // fclose(fp);

    // FILE *fp = fopen("ceshi_read.txt", "r+");
    // char temp[10] = "", c;
    // int j = 1;
    // j = &(siz[0]) - &siz[0];

    // fseek(fp, j, SEEK_SET);
    // fwrite(upd, sizeof(upd) - 1, 1, fp);

    // while ((0 == fseek(fp, -(2 * j), SEEK_END)) && fread(temp, 2, 1, fp))
    // {
    //     j++;
    //     printf("%s", temp);
    //     while ('\n' != (c = getchar()))
    //         ;
    // }

    // fclose(fp);
}
int temp()
{

    char temp[34] = "1";
    union elem_vector user;

    vector vc = vector_create(10);
    data_type data_temp;
    strcpy(data_temp._PRODUCT.ID, "1001");
    strcpy(data_temp._PRODUCT.name, "书");
    data_temp._PRODUCT.cap = 100;
    data_temp._PRODUCT.size = 88;

    vector_push_back(vc, data_temp);

    strcpy(data_temp._PRODUCT.ID, "1002");
    strcpy(data_temp._PRODUCT.name, "桌子");
    data_temp._PRODUCT.cap = 200;
    data_temp._PRODUCT.size = 11;

    vector_push_back(vc, data_temp);

    strcpy(data_temp._PRODUCT.ID, "1003");
    strcpy(data_temp._PRODUCT.name, "无良老板");
    data_temp._PRODUCT.cap = 1;
    data_temp._PRODUCT.size = 1;

    vector_push_back(vc, data_temp);

    product_Update_data(vc);
    vector_destroy(vc);

    // user_main();
     vc = vector_create(10);
    strcpy(data_temp._USER.ID, "1001");
    strcpy(data_temp._USER.name, "大网");
    strcpy(data_temp._USER.phone, "15342259102");
    strcpy(data_temp._USER.ID_card, "413025200179178316");
    strcpy(data_temp._USER.sex, "男");
    memcpy(data_temp._USER.md5_passwd, "1", sizeof(data_temp._USER.md5_passwd));

    vector_push_back(vc, data_temp);

    strcpy(data_temp._USER.ID, "1002");
    strcpy(data_temp._USER.name, "小张");
    strcpy(data_temp._USER.phone, "15342259102");
    strcpy(data_temp._USER.ID_card, "413025200179178316");
    strcpy(data_temp._USER.sex, "男");
    memcpy(data_temp._USER.md5_passwd, "1", sizeof(data_temp._USER.md5_passwd));

    vector_push_back(vc, data_temp);

    strcpy(data_temp._USER.ID, "1003");
    strcpy(data_temp._USER.name, "王老板");
    strcpy(data_temp._USER.phone, "15342259102");
    strcpy(data_temp._USER.ID_card, "413025200179178316");
    strcpy(data_temp._USER.sex, "男");
    md5_data(temp, 33, data_temp._USER.md5_passwd);

    vector_push_back(vc, data_temp);

    strcpy(data_temp._USER.ID, "////");
    strcpy(data_temp._USER.name, "boos");
    strcpy(data_temp._USER.phone, "15224722956");
    strcpy(data_temp._USER.ID_card, "413025200179178316");
    strcpy(data_temp._USER.sex, "男");
    md5_data(temp, 33, data_temp._USER.md5_passwd);

    // MD5_encryption(&data_temp);

    vector_push_back(vc, data_temp);

    user_Update_data(vc);
}

// 管理员功能
void admin_Function(vector user_data, vector product_data, elem_type *user_inf)
{
    char c, get_ch;

    while (1)
    {
        system("clear");
        printf("1.用户增删改查\n");
        printf("2.货物增删改查\n");
        printf("3.管理员信息\n");
        printf("4.设置\n"); // 设置包括设置用户信息、货物信息的文件备份数量
        printf("5.更新备份数据\n");
        printf("0.退出\n");

        printf("::>");

        get_ch = getchar();
        while ('\n' != get_ch && '\n' != (c = getchar()))
            ;

        switch (get_ch)
        {
        case '0':
            return;
            break;
        case '1':
            user_Function(user_data);
            break;
        case '2':
            product_Function(product_data);
            break;

        case '3':
            printf("\n---------管理员信息-----------\n\n");
            user_inf_upd(user_inf); // 进入修改
            break;

        case '5': // 更新备份
            printf("正在更新数据备份");
            fflush(stdout);
            sleep(1);
            printf(".");
            fflush(stdout);
            product_Update_data(product_data); // 更新货物信息
            user_Update_data(user_data);       // 更新用户信息
            sleep(1);
            printf(".");
            fflush(stdout);
            sleep(1);
            printf(".");
            fflush(stdout);
            sleep(1);
            printf("\n");

            system("clear");

            printf("更新完成(正在返回....)\n");
            sleep(2);
            break;

        default:
            printf("input error\n");
            // sleep(1);

            break;
        }
    }
}

int main()
{

    vector user_data = NULL;            // 用户数据域
    vector product_data = NULL;         // 货物数据域
    union elem_vector *user_inf = NULL; // 临时用户结构体的指针

    // if (startup_self_test())//初次启动
    // {
    //     one_start_up();
    // }

    // 必要的启动函数
    user_data = user_Init();       // 启动
    product_data = product_Init(); // 启动

    while (1)
    {
        user_inf = log_user_check(user_data); // 用户登陆检测
        if (NULL == user_inf)
        {
            printf("没有此用户！\n");
            sleep(1);
            continue;
        }
        if (0 == strcmp(user_inf->_PRODUCT.ID, "////")) // 如果是管理员
        {
            admin_Function(user_data, product_data, user_inf);
        }
        else // 否则是普通用户
        {
            BR_Main(product_data, user_inf);
        }
    }
}

// 第一次启动程序函数。
void one_start_up()
{
    FILE *fp = fopen(USER_DATA_FILE, "w");
    union elem_vector boos;
    char data[100] = "", rtn, c;
    char input[][20] = {"姓名", "电话", "身份证号", "密码"};
    int i = 0;

    if (NULL == fp)
        exit(EXIT_FAILURE);
    while (1)
    {
        system("clear");
        printf("--------------------------\n");
        printf("程序第一次进行，请根随提示输入管理员的相关信息(后可修改)\n");
        while (i < 5)
        {
            user_Vector_Upd_switch(&boos, i + 2);
            i++;
        }

        printf("确认信息?(Y/N):");

        rtn = getchar();
        while ('\n' != rtn && '\n' != (c = getchar())) // 一个回车也会进入下一循环
            ;

        if ('Y' == rtn && 'y' == rtn)
            return;
    }

    MD5_encryption(&boos); // 密码加密

    fwrite(&boos, sizeof(union elem_vector), 1, fp);
    fclose(fp);
}

// 程序启动自检
int startup_self_test(void)
{
    FILE *fp = fopen(USER_DATA_FILE, "r");

    if (NULL == fp)
        return 1;

    fclose(fp);
    return 0;
}

// 用户登陆检测函数，有该用户并且登陆成功，返回其数据域地址为后续功能提供信息
elem_type *log_user_check(vector v)
{
    char load_temp[100] = "";
    char *c = NULL;
    elem_type *inf_user = NULL;
    union elem_vector input;

    int i = 0;

    while (1)
    {
        printf("请输入用户名:");

        scanf("%s[^\n]", load_temp);
        getchar();

        if (NULL == (inf_user = user_find_data(v, load_temp, user_func_array[1], 1)))
        {
            printf("用户名不存在请重新输入!\n");
            continue;
        }

        printf("请输入用户密码：");

        for (i = 1; i < 6; i++)
        {
            memset(load_temp, '\0', sizeof(load_temp));

            input_passwd(load_temp);

            if (NULL != (c = strchr(load_temp, '\n')))
            {
                *c = '\0';
            }
            md5_data(load_temp, 33, input._USER.md5_passwd);

            // load_temp ------ MD5;
            inf_user->_USER.md5_passwd;

            if (0 == strcmp(inf_user->_USER.md5_passwd, input._USER.md5_passwd))
            {
                return inf_user;
            }
            printf("密码错误!\n");

            printf("请再次输入(%d/5):", i);
        }
        printf("用户 %s 密码多次输入错误，此事件将被上报!(回车继续)", inf_user->_PRODUCT.name);

        while ('\n' != (*c = getchar())) // 一个回车也会进入下一循环
            ;
    }

    return NULL;
}
